package com.chuanyekeji.aokai.Controller;

import com.chuanyekeji.aokai.Config.ReadPathConfig;
import com.chuanyekeji.aokai.Entity.FirstPage;
import com.chuanyekeji.aokai.Entity.InformationPage;
import com.chuanyekeji.aokai.Mapper.FirstPageMapper;
import com.chuanyekeji.aokai.Mapper.InformationPageMapper;
import com.chuanyekeji.aokai.Util.FileUtil;
import com.chuanyekeji.aokai.Util.ImageCodeUtil;
import com.chuanyekeji.aokai.Util.ReturnData;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
public class InformationPageController {

    @Autowired
    ReadPathConfig readPathConfig;

    @Autowired
    InformationPageMapper informationPageMapper;

    /**
     * 获取轮转图
     * @return
     */
    @RequestMapping("get_project_banner")
    String get_project_banner() {
        List<InformationPage> informationPageList = informationPageMapper.getInformationPageByAll();
        return ReturnData.success("success",new Gson().toJson(informationPageList));
    }

    /**
     * 保存资讯页轮转图
     * @param images
     * @return
     */
    @RequestMapping("setInformationBanner")
    String setInformationBanner(@RequestParam(value = "image",required = false) MultipartFile[] images) {
        //封装返回数据
        Map<String,Object> data = new HashMap<>();
        Gson gson = new Gson();
        //获取根目录
        String firstImagePath = "";
        try {
            firstImagePath = ResourceUtils.getURL("classpath:").getPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //获取文件上传时的项目相对路径
        File file = new File(firstImagePath,"static/" + readPathConfig.getImage());
        firstImagePath = file.getAbsolutePath() + "/";
        for(int i=0;i<images.length;i++) {
            String fileName = images[i].getOriginalFilename();
            //随机码
            String imgCode = UUID.randomUUID().toString();
            //获取点最后出现的位置
            int lastIndex = fileName.lastIndexOf(".");
            //字符串最后的位置
            int endIndex = fileName.length();
            // 获取文件后缀名
            String str = fileName.substring(lastIndex,endIndex);
            String imageName = imgCode + str;
            try {
                //保存图片
                if(FileUtil.uploadFile(images[i].getBytes(), firstImagePath,imageName)) {
                    data.put("code",0);
                    data.put("url",imageName);
                    if(informationPageMapper.countEnable() >= 5) {
                        informationPageMapper.delete();
                    }
                    InformationPage f = new InformationPage();
                    f.setIsEnable(1);
                    f.setInformationPageImage(readPathConfig.getImage()  + "/" + imageName);
                    informationPageMapper.insertSelective(f);
                } else {
                    data.put("code",1);
                    data.put("message","error");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return gson.toJson(data);
    }
}
